// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.properties.tabbed;

import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.ISection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-1-17 qiang.zhang $
 * 
 */
public class DenguesTab {

    private ISection[] sections;

    private boolean controlsCreated;

    /**
     * Qiang.Zhang.Adolf@gmail.com DenguesTab constructor comment.
     */
    public DenguesTab() {
        controlsCreated = false;
    }

    /**
     * Retrieve a numbered index for the section.
     * 
     * @param section the section.
     * @return the section index.
     */
    public int getSectionIndex(ISection section) {
        for (int i = 0; i < sections.length; i++) {
            if (section == sections[i]) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Retrieve the section at a numbered index.
     * 
     * @param i a numbered index.
     * @return the section.
     */
    public ISection getSectionAtIndex(int i) {
        if (i >= 0 && i < sections.length) {
            return sections[i];
        }
        return null;
    }

    /**
     * Retrieve the sections on the tab.
     * 
     * @return the sections on the tab.
     */
    public ISection[] getSections() {
        return sections;
    }

    /**
     * Creates page's sections controls.
     * 
     * @param parent
     * @param page
     */
    public void createControls(Composite parent, final TabbedPropertySheetWidgetFactory widgetFactory) {
        Composite pageComposite = widgetFactory.createComposite(parent, SWT.NO_FOCUS);
        GridLayout layout = new GridLayout();
        layout.marginWidth = 0;
        layout.marginHeight = 0;
        layout.verticalSpacing = 0;
        pageComposite.setLayout(layout);

        for (int i = 0; i < sections.length; i++) {
            final ISection section = sections[i];
            final Composite sectionComposite = widgetFactory.createComposite(pageComposite, SWT.NO_FOCUS);
            sectionComposite.setLayout(new FillLayout());
            int style = (section.shouldUseExtraSpace()) ? GridData.FILL_BOTH : GridData.FILL_HORIZONTAL;
            GridData data = new GridData(style);
            data.heightHint = section.getMinimumHeight();
            sectionComposite.setLayoutData(data);

            ISafeRunnable runnable = new ISafeRunnable() {

                public void run() throws Exception {
                    section.createControls(sectionComposite, null);
                }

                public void handleException(Throwable exception) {
                    /* not used */
                }
            };
            SafeRunner.run(runnable);
        }
        controlsCreated = true;
    }

    /**
     * Dispose of page's sections controls.
     */
    public void dispose() {
        for (int i = 0; i < sections.length; i++) {
            final ISection section = sections[i];
            ISafeRunnable runnable = new ISafeRunnable() {

                public void run() throws Exception {
                    section.dispose();
                }

                public void handleException(Throwable exception) {
                    /* not used */
                }
            };
            SafeRunner.run(runnable);
        }
    }

    /**
     * Sends the lifecycle event to the page's sections.
     */
    public void aboutToBeShown() {
        for (int i = 0; i < sections.length; i++) {
            final ISection section = sections[i];
            ISafeRunnable runnable = new ISafeRunnable() {

                public void run() throws Exception {
                    section.aboutToBeShown();
                }

                public void handleException(Throwable exception) {
                    /* not used */
                }
            };
            SafeRunner.run(runnable);
        }
    }

    /**
     * Sends the lifecycle event to the page's sections.
     */
    public void aboutToBeHidden() {
        for (int i = 0; i < sections.length; i++) {
            final ISection section = sections[i];
            ISafeRunnable runnable = new ISafeRunnable() {

                public void run() throws Exception {
                    section.aboutToBeHidden();
                }

                public void handleException(Throwable exception) {
                    /* not used */
                }
            };
            SafeRunner.run(runnable);
        }
    }

    /**
     * Sets page's sections input objects.
     * 
     * @param part
     * @param selection
     */
    public void setInput(final IWorkbenchPart part, final ISelection selection) {
        for (int i = 0; i < sections.length; i++) {
            final ISection section = sections[i];
            ISafeRunnable runnable = new ISafeRunnable() {

                public void run() throws Exception {
                    section.setInput(part, selection);
                }

                public void handleException(Throwable throwable) {
                    throwable.printStackTrace();
                }
            };
            SafeRunner.run(runnable);
        }
    }

    public void setSections(ISection[] sections) {
        this.sections = sections;
    }

    /**
     * 
     * @return <code>true</code> if controls have been created.
     */
    public boolean controlsHaveBeenCreated() {
        return controlsCreated;
    }

    /**
     * If controls have been created, refresh all sections on the page.
     */
    public void refresh() {
        if (controlsCreated) {
            for (int i = 0; i < sections.length; i++) {
                final ISection section = sections[i];
                ISafeRunnable runnable = new ISafeRunnable() {

                    public void run() throws Exception {
                        section.refresh();
                    }

                    public void handleException(Throwable throwable) {
                        throwable.printStackTrace();
                    }
                };
                SafeRunner.run(runnable);
            }
        }
    }
}
